package indi.zhifa.engine.test.controller;

import com.alibaba.fastjson2.JSONObject;
import indi.zhifa.engine.core.common.web.ann.ZhiFaRestController;
import indi.zhifa.engine.core.common.web.utils.WebCommonUtil;
import indi.zhifa.engine.redis.utils.RedisTemplateProvider;
import indi.zhifa.engine.redis.enums.ERedisType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@Tag(name = "redis测试")
@RequiredArgsConstructor
@ZhiFaRestController
@RequestMapping("/api/test/redis")
public class RedisTestController {

    final RedisTemplateProvider mProvider;

    @Operation(summary = "获取数据")
    @GetMapping("/{redisType}/{key}")
    public JSONObject get(@PathVariable(name = "redisType") ERedisType redisType, @PathVariable(name = "key") String key) {
        RedisTemplate<String, Object> redisTemplate = mProvider.getRedisTemplate(redisType);
        Object value = redisTemplate.opsForValue().get(key);
        return JSONObject.from(value);
    }

    @Operation(summary = "获设置数据")
    @PutMapping("/{redisType}/{key}")
    public void put(@PathVariable(name = "redisType") ERedisType redisType, @PathVariable(name = "key") String key, @RequestBody JSONObject value) {
        RedisTemplate<String, Object> redisTemplate = mProvider.getRedisTemplate(redisType);
        redisTemplate.opsForValue().set(key,value);
    }

    @Operation(summary = "获得session信息")
    @GetMapping("/session/getSessionId")
    public String getSessionId(){
       String sessionId = WebCommonUtil.getServletRequest().getSession().getId();
       return sessionId;
    }

}
